package com.skiving.cloud.workflow.controller;

import com.mybatisflex.core.paginate.Page;
import com.skiving.cloud.auth.logutils.OperateLog;
import com.skiving.cloud.common.utils.PageParameter;
import com.skiving.cloud.common.utils.ReturnT;
import com.skiving.cloud.common.utils.UuidUtil;
import com.skiving.cloud.workflow.constants.WorkflowConstants;
import com.skiving.cloud.workflow.entity.WfStrategy;
import com.skiving.cloud.workflow.service.WfStrategyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.noear.solon.annotation.*;
import org.noear.solon.validation.annotation.Validated;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * 审批人找人策略 控制层。
 *
 * @author skivingcloud
 * @since 2024-05-17
 */
@Controller
@Api("审批人找人策略接口")
@Mapping(WorkflowConstants.WORKFLOW_BASE_URL + "/wfStrategy")
public class WfStrategyController {

    private static final Logger log = LoggerFactory.getLogger(WfStrategyController.class);

    @Inject
    private WfStrategyService wfStrategyService;

    /**
     * 添加审批人找人策略。
     *
     * @param wfStrategy 审批人找人策略
     * @return {@code {code:0,message:"",data:true}} 添加成功，{@code {code:500,message:"",data:false}} 添加失败
     */
    @Post
    @Mapping("/save")
    @ApiOperation("保存审批人找人策略")
    @OperateLog(module="流程管理-审批人找人策略", description = "保存审批人找人策略")
    public ReturnT<Boolean> save(@Validated @Body @Param("审批人找人策略") WfStrategy wfStrategy) {
        try{
            wfStrategy.setId(UuidUtil.get32UUID());
            return ReturnT.ok(wfStrategyService.save(wfStrategy));
        }catch(Exception e){
            log.error("新增审批人找人策略失败", e);
            return ReturnT.error("新增审批人找人策略失败");
        }
    }

    /**
     * 根据主键删除审批人找人策略。
     *
     * @param id 主键
     * @return {@code {code:0,message:"",data:true}} 删除成功，{@code {code:500,message:"",data:false}} 删除失败
     */
    @Post
    @Mapping("/remove/{id}")
    @ApiOperation("根据主键删除审批人找人策略")
    @OperateLog(module="流程管理-审批人找人策略", description = "删除审批人找人策略")
    public ReturnT<Boolean> remove(@Path("id") @Param("审批人找人策略主键")String id) {
        try{
            return ReturnT.ok(wfStrategyService.removeById(id));
        }catch(Exception e){
            log.error("删除审批人找人策略失败", e);
            return ReturnT.error("删除审批人找人策略失败");
        }
    }

    /**
     * 根据主键更新审批人找人策略。
     *
     * @param wfStrategy 审批人找人策略
     * @return {@code {code:0,message:"",data:true}} 更新成功，{@code {code:500,message:"",data:false}} 更新失败
     */
    @Post
    @Mapping("/update")
    @ApiOperation("根据主键更新审批人找人策略")
    @OperateLog(module="流程管理-审批人找人策略", description = "修改审批人找人策略")
    public ReturnT<Boolean> update(@Validated @Body @Param("审批人找人策略主键")WfStrategy wfStrategy) {
        try{
            return ReturnT.ok(wfStrategyService.updateById(wfStrategy));
        }catch(Exception e){
            log.error("保存审批人找人策略失败", e);
            return ReturnT.error("保存审批人找人策略失败！");
        }
    }

    /**
     * 根据审批人找人策略主键获取详细信息。
     *
     * @param id 审批人找人策略主键
     * @return 审批人找人策略详情
     */
    @Get
    @Mapping("/getInfo/{id}")
    @ApiOperation("根据主键获取审批人找人策略")
    public ReturnT<WfStrategy> getInfo(@Path("id") String id) {
        return ReturnT.ok(wfStrategyService.getById(id));
    }

    /**
     * 分页查询审批人找人策略。
     *
     * @param parameter 分页对象
     * @return 分页对象
     */
    @Post
    @Mapping("/page")
    @ApiOperation("分页查询审批人找人策略")
    public ReturnT<Page<WfStrategy>> page(@Body @Param("分页信息") PageParameter<WfStrategy> parameter) {
        Page<WfStrategy> page = wfStrategyService.queryByPage(parameter);
        return ReturnT.ok(page);
    }
    
    /**
     * 查询策略{key，value}
     * @return 用户信息
     */
    @ApiOperation("查询策略")
    @Get
    @Mapping("/getWfStrategyKeyValue")
    public ReturnT<List<Map<String, Object>>> getWfStrategyKeyValue(){
        return ReturnT.ok(wfStrategyService.getWfStrategyKeyValue());
    }
    
}